package com.lennon.oauth.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lennon.oauth.constants.GrantTypeEnum;
import com.lennon.oauth.dto.AuthorizeDTO;
import com.lennon.oauth.dto.AuthorizeTokenDTO;
import com.lennon.oauth.message.ModelMapUtil;
import com.lennon.oauth.message.ResultMessage;
import com.lennon.oauth.message.ResultObject;
import com.lennon.oauth.store.StoreFactory;

@Controller
@RequestMapping(value = "/oauth")
public class OAuth2Controller {

	@RequestMapping(value = "/authorize")
	public String authorize(ModelMap modelMap, AuthorizeDTO authorizeDTO) {
		
		// 如果是授权码模式
		if(GrantTypeEnum.AUTHORIZATION_CODE.getValue().equals(authorizeDTO.getResponse_type())) {
			// 检验客户信息
			if(!StoreFactory.getClientStore().isContainsClientId(authorizeDTO.getClient_id())) {
				ModelMapUtil.setMessage(modelMap, ResultMessage.ERROR_CLIENT_ID);
				return returnErrorPage();
			}
			// 检验重定向地址
			if(!StoreFactory.getClientStore().isContainsRedirectUri(authorizeDTO.getClient_id(), authorizeDTO.getRedirect_uri())) {
				ModelMapUtil.setMessage(modelMap, ResultMessage.ERROR_REDIRECT_URI);
				return returnErrorPage();
			}
			
			modelMap.put("client_id", authorizeDTO.getClient_id());
			modelMap.put("redirect_uri", authorizeDTO.getRedirect_uri());
			modelMap.put("state", authorizeDTO.getState());
		}
		
		return "/auth";
	}
	
	@RequestMapping(value = "/confirm")
	public String accessConfirm(ModelMap modelMap, AuthorizeDTO authorizeDTO) {
		
		// 检验客户信息
		if(!StoreFactory.getClientStore().isContainsClientId(authorizeDTO.getClient_id())) {
			ModelMapUtil.setMessage(modelMap, ResultMessage.ERROR_CLIENT_ID);
			return returnErrorPage();
		}
		// 检验重定向地址
		if(!StoreFactory.getClientStore().isContainsRedirectUri(authorizeDTO.getClient_id(), authorizeDTO.getRedirect_uri())) {
			ModelMapUtil.setMessage(modelMap, ResultMessage.ERROR_REDIRECT_URI);
			return returnErrorPage();
		}
		
		// 根据填写的回调地址回调回去
		return "redirect:" + authorizeDTO.getRedirect_uri()+"?code="+StoreFactory.getCodeStore().createUUIDCode(authorizeDTO.getClient_id())
			+"&state="+authorizeDTO.getState();
	}
	
	@RequestMapping(value = "/token")
	@ResponseBody
	public ResultObject accessToken(ModelMap modelMap, AuthorizeTokenDTO authorizeTokenDTO) {
		
		// 检验客户信息
		if(!StoreFactory.getClientStore().isConatinsClient(authorizeTokenDTO.getClient_id(), authorizeTokenDTO.getClient_secret())) {
			return ResultMessage.ERROR_CLIENT_ID.getResultObject();
		}
		
		// 检验重定向地址
		if(!StoreFactory.getClientStore().isContainsRedirectUri(authorizeTokenDTO.getClient_id(), authorizeTokenDTO.getRedirect_uri())) {
			return ResultMessage.ERROR_REDIRECT_URI.getResultObject();
		}
		
		// 检验code
		if(!StoreFactory.getCodeStore().isRightCode(authorizeTokenDTO.getCode(), authorizeTokenDTO.getClient_id())) {
			return ResultMessage.ERROR_CODE.getResultObject();
		}
		
		// 生成token
		if(GrantTypeEnum.AUTHORIZATION_CODE.equals(authorizeTokenDTO.getGrant_type())) {
			// 也可以根据redirect_uri 回调回去
			// 也可以将返回值包装成Josn返回
			// 
			return ResultMessage.SUCCESS.getResultObject(StoreFactory.getTokenStore().createUUIDToken(authorizeTokenDTO.getClient_id()));
			
		}
		
		// 刷新token
		if(GrantTypeEnum.REFRESH_TOKEN.equals(authorizeTokenDTO.getGrant_type())) {
			// 拿到refreshToken 并检验刷新
			// 这里没有做实现，但是原理一致
			return ResultMessage.SUCCESS.getResultObject(StoreFactory.getTokenStore().createUUIDToken(authorizeTokenDTO.getClient_id()));
		}
		
		return ResultMessage.ERROR_GRANT_TYPE.getResultObject();
	}
	
	/**
	 * 返回错误页面地址
	 * @return
	 */
	private static String returnErrorPage() {
		return "error";
	}
}
